import logging,os
from support.const.state import *
class Log:
    @staticmethod
    def _init_system(path):
        path = os.path.join(path, 'log', 'system')
        Log._autoCreateDir(path)
        fmt = logging.Formatter('[%(asctime)s] %(message)s')
        criticalFileHandler = logging.FileHandler(os.path.join(path, 'critical.log'))
        errorFileHandler = logging.FileHandler(os.path.join(path, 'error.log'))
        warningFileHandler = logging.FileHandler(os.path.join(path, 'warning.log'))
        infoFileHandler = logging.FileHandler(os.path.join(path, 'info.log'))
        debugFileHandler = logging.FileHandler(os.path.join(path, 'debug.log'))
        criticalHandler = logging.StreamHandler()
        errorHandler = logging.StreamHandler()
        criticalFileHandler.setLevel(logging.CRITICAL)
        errorFileHandler.setLevel(logging.ERROR)
        warningFileHandler.setLevel(logging.WARNING)
        infoFileHandler.setLevel(logging.INFO)
        debugFileHandler.setLevel(logging.DEBUG)
        criticalHandler.setLevel(logging.CRITICAL)
        errorHandler.setLevel(logging.ERROR)
        criticalFileHandler.setFormatter(fmt)
        errorFileHandler.setFormatter(fmt)
        warningFileHandler.setFormatter(fmt)
        infoFileHandler.setFormatter(fmt)
        debugFileHandler.setFormatter(fmt)
        criticalHandler.setFormatter(fmt)
        errorHandler.setFormatter(fmt)
        logging.getLogger("airtest").setLevel(Global.DEBUG_AIRTEST_MESSAGE_LEVEL)
        logging.getLogger().setLevel(logging.ERROR)
        logging.getLogger().addHandler(criticalFileHandler)
        logging.getLogger().addHandler(errorFileHandler)
        logging.getLogger().addHandler(warningFileHandler)
        logging.getLogger().addHandler(infoFileHandler)
        logging.getLogger().addHandler(debugFileHandler)
        logging.getLogger().addHandler(criticalHandler)
        logging.getLogger().addHandler(errorHandler)
    @staticmethod
    def print(_type, msg):
        _type = _type.upper()
        if(_type == 'DEBUG'):
            logging.debug(msg)
        elif(_type == 'WARNING'):
            logging.warning(msg)
        elif(_type == 'ERROR'):
            logging.error(msg)
        elif(_type == 'CRITICAL'):
            logging.critical(msg)
        else:
            logging.info(msg)
    def __init__(self, path, clazzname):
        path = os.path.join(path, 'log')
        self._autoCreateDir(path)
        self.loger = logging.getLogger('ray_{}'.format(clazzname))
        self.loger.setLevel(logging.DEBUG)
        filehandler = logging.FileHandler(os.path.join(path, '{}.log'.format(clazzname)))
        handler = logging.StreamHandler()
        handler.setLevel(Global.DEBUG_MESSAGE_LEVEL)        
        fmt = logging.Formatter('[%(asctime)s][{}] %(message)s'.format(clazzname))
        filehandler.setFormatter(fmt)
        handler.setFormatter(fmt)
        self.loger.addHandler(filehandler)
        self.loger.addHandler(handler)                
    def debug(self, msg):
        self.loger.debug(msg)
    def error(self, msg):
        self.loger.error(msg)
    def info(self, msg):
        self.loger.info(msg)
    def warning(self, msg):
        self.loger.warning(msg)
    @staticmethod
    def _autoCreateDir(*dirs):
        path = ''
        for dir in dirs:
            path = os.path.join(path,dir)
        if(not os.path.exists(path)):
            os.makedirs(path)
        return path